<template>
  <view class="register-page">

     <header class="header">
      <image class="header-image" src="/static/header-bg.png" mode="aspectFill"></image>
      
      <view class="hero-content">
        <!-- 左侧文本区域 -->
        <view class="hero-text">
          <text class="welcome">HI,欢迎新朋友~</text>
          <text class="slogan">好玩世界，尽在社交搭子</text>
        </view>
        
        <!-- 右侧图片区域 -->
        <view class="hero-image">
          <image src="/static/welcome-image.png" mode="aspectFit"></image>
        </view>
      </view>
    </header>

    <!-- 表单卡片 -->
    <view class="card">
      <view class="field">
        <text class="label">你的昵称</text>
        <input class="input" v-model="nickname" type="text" placeholder="请输入昵称" maxlength="20" />
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">你的手机号</text>
        <input class="input" v-model="phone" type="number" placeholder="请输入手机号" maxlength="11" />
      </view>
      <view class="divider"></view>
      
      <view class="field row">
        <view class="col">
          <text class="label">验证码</text>
          <input class="input" v-model="code" type="number" placeholder="请输入验证码" maxlength="6" />
        </view>
        <button class="code-btn" :disabled="countdown>0" @click="onGetCode">
          {{ countdown>0 ? `${countdown}s` : '获取验证码' }}
        </button>
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">你的密码</text>
        <input class="input" v-model="password" type="password" placeholder="请输入至少6位数字+字母密码" />
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">确认密码</text>
        <input class="input" v-model="confirmPassword" type="password" placeholder="请再次输入密码" />
      </view>
      <view class="divider"></view>
      
      <view class="field">
        <text class="label">你的年龄</text>
        <input class="input" v-model="age" type="number" placeholder="请输入年龄" maxlength="3" />
      </view>
    </view>

    <view class="gender-card">
      <view class="gender-field">
        <view class="gender-select">
          <view class="gender-option" :class="{ active: gender === 'boy' }" @click="gender = 'boy'">
            <image class="gender-icon" src="/static/boy-icon.png" mode="aspectFit"></image>
            <view class="selected-icon" v-if="gender === 'boy'">
              <image src="/static/selected-icon.png" mode="aspectFit"></image>
            </view>
            <text class="gender-text">Boy</text>
          </view>
          <view class="gender-option" :class="{ active: gender === 'girl' }" @click="gender = 'girl'">
            <image class="gender-icon" src="/static/girl-icon.png" mode="aspectFit"></image>
            <view class="selected-icon" v-if="gender === 'girl'">
              <image src="/static/selected-icon.png" mode="aspectFit"></image>
            </view>
            <text class="gender-text">Girl</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 协议与按钮 -->
    <view class="agreement">
      <view class="checkbox" :class="{ checked: agreed }" @click="agreed = !agreed"></view>
      <text>我已阅读并同意</text>
      <text class="link" @click="onAgreementClick(1)">《用户服务协议》</text>
      <text>、</text>
      <text class="link" @click="onAgreementClick(2)">《隐私协议》</text>
    </view>

    <button class="register-btn" @click="onRegister">
      注册
    </button>

    <view class="login" @click="goLogin">已有账号？去登录</view>
  </view>
</template>

<script setup>
  import { ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import AuthUtil from '@/sheep/api/member/auth';

  const nickname = ref('');
  const phone = ref('');
  const code = ref('');
  const password = ref('');
  const confirmPassword = ref('');
  const age = ref('');
  const gender = ref('boy'); // 默认选中男性
  const agreed = ref(false);
  const countdown = ref(0);
  let timer = null;

  // 密码验证函数
  const validatePassword = (pwd) => {
    if (pwd.length < 6) {
      return '密码至少需要6位';
    }
    if (!/[a-zA-Z]/.test(pwd) || !/\d/.test(pwd)) {
      return '密码必须包含数字和字母';
    }
    return null;
  };

  const onGetCode = () => {
    if (countdown.value > 0) return;
    if (phone.value.length !== 11) {
      return uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    }
    
    AuthUtil.sendSmsCode(phone.value, 5)
      .then(res => {
        if (res.code === 0) {
          uni.showToast({ title: '验证码已发送', icon: 'success' });
          // 开始倒计时
          countdown.value = 60;
          timer = setInterval(() => {
            countdown.value--;
            if (countdown.value <= 0) {
              clearInterval(timer);
              timer = null;
            }
          }, 1000);
        } else {
          uni.showToast({ title: res.msg || '发送失败', icon: 'none' });
        }
      })
      .catch(error => {
        console.error('发送验证码失败:', error);
        uni.showToast({ title: '发送失败，请重试', icon: 'none' });
      });
  };

  const onRegister = () => {
    // 表单验证
    if (!nickname.value) {
      return uni.showToast({ title: '请输入昵称', icon: 'none' });
    }
    if (phone.value.length !== 11) {
      return uni.showToast({ title: '请输入正确手机号', icon: 'none' });
    }
    if (!code.value) {
      return uni.showToast({ title: '请输入验证码', icon: 'none' });
    }
    const passwordError = validatePassword(password.value);
    if (passwordError) {
      return uni.showToast({ title: passwordError, icon: 'none' });
    }
    if (password.value !== confirmPassword.value) {
      return uni.showToast({ title: '两次密码输入不一致', icon: 'none' });
    }
    if (!age.value) {
      return uni.showToast({ title: '请输入年龄', icon: 'none' });
    }
    if (!gender.value) {
      return uni.showToast({ title: '请选择性别', icon: 'none' }); // 新增性别验证
    }
    if (!agreed.value) {
      return uni.showToast({ title: '请同意用户协议', icon: 'none' });
    }

    // 准备注册数据
    const registerData = {
      nickname: nickname.value,
      username: nickname.value,
      mobile: phone.value,
      password: password.value,
      code: code.value,
      age: age.value ? parseInt(age.value) : undefined,
      sex: gender.value === 'boy' ? 1 : 2 // 男性为1，女性为2
    };

    // 调用注册接口
    AuthUtil.register(registerData)
      .then(res => {
        if (res.code === 0) {
          uni.showToast({ title: '注册成功', icon: 'success' });
          // 注册成功后跳转到登录页面或其他页面
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        } else {
          uni.showToast({ title: res.msg || '注册失败', icon: 'none' });
        }
      })
      .catch(error => {
        console.error('注册失败:', error);
        uni.showToast({ title: '注册失败，请重试', icon: 'none' });
      });
  };

  const goLogin = () => {
    uni.navigateBack();
  };

  onLoad(() => {
    // 页面加载时的初始化逻辑
  });

  // 协议点击事件
  const onAgreementClick = (id) => {
    uni.navigateTo({ 
      url: `/pages/index/agreement?id=${id}`
    });
  };
</script>

<style scoped>  
  .register-page {
    min-height: 100vh;
    background: #F6F6F6;
    padding: 0 0 40rpx 0;
  }
  .header {
    position: relative;
    height: 480rpx;
    width: 100%;
    margin-bottom: 0;
  }
  .header-image {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
  }
  .hero-content {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0rpx 50rpx;
    z-index: 20;
  }
  
  /* 左侧文本样式 */
  .hero-text {
    flex: 1;
    padding-right: 30rpx;
  }
  .welcome { 
    display: block; 
    font-size: 48rpx; 
    font-weight: 800; 
    color: #000000; 
    margin-bottom: 12rpx; 
    margin-top: -250rpx;
  }
  .slogan { 
    display: block; 
    font-size: 26rpx; 
    color: #666; 
  }
  .hero-image {
    width: 321rpx;
    height: 306rpx;
    margin-top: -300rpx;
    margin-right: 53rpx;
  }
  .hero-image image {
    width: 100%;
    height: 100%;
    border-radius: 24rpx;
  }
  .card { 
    background: #fff; 
    border-radius: 20rpx; 
    padding: 24rpx; 
    margin-top: -40rpx; 
    margin-left: 17rpx; 
    margin-right: 17rpx;
    z-index: 30;
    position: relative;
  }
  .field {
    margin-top: 20rpx;
    margin-bottom: 8rpx; 
  }
  .row { 
    display: flex; 
    align-items: center; 
    gap: 16rpx; 
  }
  .col { 
    flex: 1; 
  }
  .label { 
    display: block; 
    font-size: 30rpx; 
    font-weight: 700; 
    color: #1f2937; 
    padding: 10rpx 20rpx;
  }
  .input { 
    width: 100%; 
    height: 80rpx; 
    border: 0; 
    background: transparent; 
    font-size: 28rpx; 
    color: #111; 
    padding: 0rpx 20rpx;
  }
  .input::placeholder {
    color: #c0c4cc; 
  }
  .divider { 
    height: 2rpx; 
    background: #f1f2f6; 
    margin: 8rpx 20rpx 20rpx 20rpx; 
  }
  .code-btn { 
    padding: 0 20rpx; 
    height: 72rpx; 
    line-height: 72rpx; 
    border-radius: 36rpx; 
    color: #fff; 
    font-size: 26rpx; 
    background: linear-gradient(90deg,#a78bfa,#60a5fa); 
    margin-right: 15rpx;
  }
  .code-btn[disabled] { 
    opacity: .5; 
  }

  /* 性别选择卡片样式（卡片外部） */
  .gender-field {
    margin-top: 20rpx;
    margin-bottom: 8rpx; 
  }
  .gender-select {
    display: flex;
    justify-content: center; 
    gap: 40rpx;
    padding: 10rpx 20rpx;
  }
  .gender-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;
    transition: all 0.3s ease;
    position: relative;
  }
  .gender-icon {
    width: 160rpx;
    height: 160rpx;
    margin-bottom: 10rpx;
  }
  .gender-text {
    font-size: 26rpx;
    color: #374151;
  }

  .selected-icon {
    position: absolute;
    bottom: 60rpx;
    right: 30rpx;
    width: 40rpx;
    height: 40rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
  }
  .selected-icon image {
    width: 50rpx;
    height: 50rpx;
  }

  .agreement { 
    display: flex; 
    justify-content: center;
    gap: 10rpx; color: #8a8f98; 
    font-size: 24rpx; 
    margin-top: 40rpx;
    margin-bottom: 40rpx;
  }
  .checkbox { 
    width: 34rpx; 
    height: 34rpx; 
    border-radius: 50%; 
    border: 2rpx solid #cbd5e1; background: #fff; 
    display: inline-flex; 
    align-items: center; 
    justify-content: center; 
  }
  .checkbox.checked { 
    background: linear-gradient(90deg,#a78bfa,#60a5fa); 
    border-color: transparent; 
  }
  .link { 
    color: #4f46e5; 
  }

  .register-btn { 
    width: 85%; 
    height: 96rpx; 
    line-height: 96rpx; 
    border-radius: 56rpx; 
    color: #fff; 
    font-size: 30rpx; 
    background: linear-gradient(90deg, #B276FF 0%, #74A7FF 100%); 
    border: none;
    outline: none;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    box-shadow: 0 10rpx 24rpx rgba(116, 167, 255, 0.35);
  }
  .login { 
    text-align: center; 
    color: #9aa1aa; 
    margin-top: 40rpx; 
    font-size: 26rpx; 
  }
</style>